package com.hqyj.shop.controller;

import com.hqyj.shop.pojo.UserInfo;
import com.hqyj.shop.service.UserInfoService;
import com.hqyj.shop.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目:shop-admin-240701
 * 描述:
 * 时间:2024/10/8 11:05
 * 作者:admin
 * 版本:1.0
 **/
@RestController
@RequestMapping("/user")
public class UserInfoController {

    //注入userInfoService
    @Autowired
    UserInfoService userInfoService;

    //账号登录
    @PostMapping("/login")
    public ResultVo login(@RequestBody UserInfo u){
        return userInfoService.login(u);
    }

    //获取验证码
    @PostMapping("/getCode")
    public ResultVo getCode(@RequestBody @Valid UserInfo userInfo, BindingResult result){
        //判断验证是否合法
        if(result.hasErrors()){
            //获取提示消息，并且传给前端
            String message = null;
            for(ObjectError obj :result.getAllErrors()){
                 message = obj.getDefaultMessage();
            }
            return ResultVo.error(message);
        }

        return userInfoService.getCode(userInfo);
    }

    //邮箱注册
    @PostMapping("/registerByEmail")
    public ResultVo registerByEmail(@RequestBody @Valid UserInfo userInfo, BindingResult result){
        //判断验证是否合法
        if(result.hasErrors()){
            //获取提示消息，并且传给前端
            String message = null;
            for(ObjectError obj :result.getAllErrors()){
                message = obj.getDefaultMessage();
            }
            return ResultVo.error(message);
        }

        return userInfoService.registerByEmail(userInfo);
    }


}
